//
//  LLYFourthViewController.m
//  ZZ15091031LiuLeiYan
//
//  Created by qianfeng001 on 15/8/28.
//  Copyright (c) 2015年 刘雷岩. All rights reserved.
//

#import "LLYFourthViewController.h"

#define randomColor arc4random()%256/255.0

@interface LLYFourthViewController ()
{
    UILabel *_label;
    
    NSTimer *_timer;
    
    UIScrollView *_scrollView;
}

@end

@implementation LLYFourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createUI];
//    [self createScrollView];
    
}

//- (void) createScrollView {
//    
//}

- (void)createUI {
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    startBtn.frame = CGRectMake(70, 50, 70, 50);
    startBtn.backgroundColor = [UIColor blackColor];
    [startBtn setTitle:@"start" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startBtn];
    
    UIButton *stopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    stopBtn.frame = CGRectMake(220, 50, 70, 50);
    stopBtn.backgroundColor = [UIColor blackColor];
    [stopBtn setTitle:@"stop" forState:UIControlStateNormal];
    [stopBtn addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:stopBtn];
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(150, 50, 50, 50)];
    _label.text = @"0";
    _label.font = [UIFont systemFontOfSize:25];
    _label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];
    
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, 375, 300)];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    NSArray *ary=@[@"13493823561.jpg",@"134750028071.jpg",@"134797593191.jpg"];
    for (int i=0; i<3; i++) {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*375, 0, 375, 300)];
        imageView.image=[UIImage imageNamed:ary[i]];
        [_scrollView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*375, 0, 375, 300)];
        label.text = [NSString stringWithFormat:@"%d",i+1];
        //label.backgroundColor = [UIColor colorWithRed:randomColor green:randomColor blue:randomColor alpha:randomColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:40];
        [_scrollView addSubview:label];
    }
    _scrollView.contentSize = CGSizeMake(3*375, 300);
    [self.view addSubview:_scrollView];

}

- (void)start {
    
//    [_timer invalidate];
//    _timer = nil;
//    
//    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    if (!_timer) {
        [_timer invalidate];
        _timer=[NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    }
}

- (void)stop {
    [_timer invalidate];
    _timer = nil;
    
    int n = [_label.text intValue];
    [_scrollView setContentOffset:CGPointMake((n-1)*375, 0) animated:YES];
//    [_scrollView removeFromSuperview];
//    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, 375, 300)];
//    _scrollView.pagingEnabled = YES;
//    _scrollView.bounces = NO;
//    for (int i=0; i<n; i++) {
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*375, 0, 375, 300)];
//        label.text = [NSString stringWithFormat:@"%d",i+1];
//        label.backgroundColor = [UIColor colorWithRed:randomColor green:randomColor blue:randomColor alpha:randomColor];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.font = [UIFont systemFontOfSize:40];
//        [_scrollView addSubview:label];
//    }
//    _scrollView.contentSize = CGSizeMake(n*375, 300);
//    [self.view addSubview:_scrollView];
}

- (void)onTimer:(NSTimer *)timer {
    
    int n = [_label.text intValue];
    if (n == 2) {
        _label.text = @"0";
    }else {
        _label.text = [NSString stringWithFormat:@"%d",n+1];
    }
    [_scrollView setContentOffset:CGPointMake((n+1)*375, 0) animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
